package com.xmu.prcs.action;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

/**
 * 基础的action类
 * @author frequent
 * @version 1.0,2012-3-20
 *
 */
@SuppressWarnings("serial")
public class BaseAction extends ActionSupport {
	public static String CANCEL = "cancel";

	public static String LIST = "list";

	protected static HttpServletRequest getRequest2() {
		return ServletActionContext.getRequest();
	}

	protected String action;
	protected String from;
	private String MESSAGES = "messages";

	protected String method;

	public BaseAction() {
		super();
	}

	public String getAction() {
		return action;
	}

	public String getFrom() {
		return from;
	}

	public String getMethod() {
		return method;
	}

	protected HttpServletRequest getRequest() {
		return ServletActionContext.getRequest();
	}

	protected HttpServletResponse getResponse() {
		return ServletActionContext.getResponse();
	}

	protected HttpSession getSession() {
		return getRequest().getSession();
	}

	@SuppressWarnings("unchecked")
	protected void saveMessage(String message) {
		List<String> messages = (List<String>) getSession().getAttribute(
				MESSAGES);
		if (null == messages) {
			messages = new ArrayList<String>();
		}
		messages.add(message);
		getSession().setAttribute(MESSAGES, messages);
	}

	protected Date stringToDate(String str) throws ParseException {
		DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		return format.parse(str);
	}
}
